#include "selscene.h"

SelScene::SelScene(QWidget *parent) : MyMainWindow(parent)
{
    this->setWindowTitle("选择关卡");

    //按钮
    backButton = new MyButton(":/res/BackButton.png", ":/res/BackButtonSelected.png", this);
    backButton->move(this->width() - backButton->width(),
                     this->height() - backButton->height());
    backButton->resize(72, 32);
    backButton->move(this->width() - backButton->width(),
                     this->height() - backButton->height());

    //关卡按钮20个
    const int colWidth = 70;
    const int rowHeight = 70;
    //x y 偏移
    const int xOffset = 25;
    const int yOffset = 130;
    //添加关卡按钮
    for(int i = 0; i < 20; i++){
        MyButton *lvButton = new MyButton(":/res/LevelIcon.png", ":/res/LevelIcon.png", this);
        int row = i / 4;
        int col = i % 4;
        int x = col * colWidth + xOffset;
        int y = row * rowHeight + yOffset;
        lvButton->setText(QString::number(i + 1));
        lvButton->resize(57, 57);
        lvButton->move(x, y);

        //跳转游戏页面
        connect(lvButton, &MyButton::clicked, [=](){
            //音效
            SoundUtil s;
            s.actSound();
            PlayScene * ps = new PlayScene(i + 1, this);
            ps->setAttribute(Qt::WA_DeleteOnClose);
            ps->move(this->pos());
            ps->show();
            this->hide();
            //显示游戏关卡页面
            connect(ps, &PlayScene::backButtonSig, [=](){
                //音效
                SoundUtil s;
                s.backSound();
                this->move(ps->pos());
                this->show();
                ps->close();
            });
        });
    }

    //返回按钮的事件绑定
    connect(backButton, &QPushButton::clicked, this, &SelScene::backButtonSig);
}

void SelScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //移动画家位置
    painter.translate(0, this->menuBar()->height());
    QPixmap pm(":/res/OtherSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pm);
    pm.load(":/res/Title.png");
    painter.drawPixmap(this->width() / 2 - pm.width() / 2, 0, pm);
}
